package com.light.gamecomponents;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;

import android.graphics.Color;
import android.graphics.Typeface;

import com.badlogic.gdx.math.Vector2;
import com.light.gamecomponents.physics.RollerPhysicsWorld;
import com.light.roller.Player;
import com.light.roller.Roller;

public class FlyingText implements IGameComponents, IUpdateHandler{

	protected Roller m_game;
	protected Vector2 m_position;
	protected Player m_player;
	protected Vector2 m_triggerPosition;
	protected Vector2 m_triggerRectangle;
	protected Texture m_fontTexture;
	protected Font m_font;
	protected ChangeableText m_printText;
	protected String m_text;
	protected Scene m_scene;
	protected RollerPhysicsWorld m_physicsWorld;
	protected float m_timer;
	protected boolean m_triggered = false;
	protected float[] m_positionXs = { 70,
			   20,
			   Roller.CAMERA_WIDTH - 70,
			   Roller.CAMERA_WIDTH - 20 };

	protected float[] m_positionYs = { 20,
			   Roller.CAMERA_HEIGHT - 70,
			   Roller.CAMERA_HEIGHT - 20,
			   70 };
	
	public FlyingText(Roller game, Vector2 triggerPosition, Vector2 triggerRectangle ,String text) {
		m_triggerPosition = triggerPosition;
		m_triggerRectangle = triggerRectangle;
		m_text = text;
		m_game = game;
		m_position = new Vector2(m_positionXs[0], m_positionYs[0]);
	}
	@Override
	public void onLoadEngine() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onLoadResources() {
		this.m_fontTexture = new Texture(256, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.m_font = new Font(this.m_fontTexture, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 20, true, Color.GREEN);
		m_game.getEngine().getTextureManager().loadTexture(this.m_fontTexture);
		m_game.getEngine().getFontManager().loadFont(this.m_font);
		
	}

	@Override
	public void unLoadResources() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onLoadScene(Scene scene, PhysicsWorld physicsWrold) {
		
		m_printText = new ChangeableText(m_position.x, m_position.y, m_font,
									     m_text, 512);
		m_printText.setBlendFunction(GL10.GL_SRC_ALPHA,
									 GL10.GL_ONE_MINUS_SRC_ALPHA);
		m_printText.setAlpha(0.00001f);
		m_printText.setVisible(false);
		scene.attachChild(m_printText);
		m_scene = scene;
		m_player = Roller.player;
		m_physicsWorld = ((RollerPhysicsWorld)physicsWrold);
		m_physicsWorld.registerUpdateHandler(this);
		
	}

	@Override
	public void removeFromScene(Scene scene, PhysicsWorld physicsWorld) {
		scene.detachChild(m_printText);
		m_printText = null;
		
	}

	@Override
	public void rotate(float angle) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onUpdate(float pSecondsElapsed) {
		
		if (m_player.getX() >= m_triggerPosition.x && 
			m_player.getX() <= m_triggerPosition.x + m_triggerRectangle.x &&
			m_player.getY() >= m_triggerPosition.y &&
			m_player.getY() <= m_triggerPosition.y + m_triggerRectangle.y &&
			!m_triggered &&
			Roller.PreNum != Roller.CurrentNum)
		{
			m_physicsWorld.unregisterUpdateHandler(this);
			m_physicsWorld.registerUpdateHandler(this);
			trigger();
		}
		if (m_triggered)
		{
			if (m_printText.getAlpha() != 0)
			{
				switch (Roller.nowDirection)
				{
				case UP:
					m_position = new Vector2(m_positionXs[0], m_positionYs[0]);
					m_printText.setRotationCenter(0, 0);
					m_printText.setRotation(0.0f);
					break;
				case DOWN:
					m_position = new Vector2(m_positionXs[2], m_positionYs[2]);
					m_printText.setRotationCenter(0, 0);
					m_printText.setRotation(180.0f);
					break;
				case RIGHT:
					m_position = new Vector2(m_positionXs[1], m_positionYs[1]);
					m_printText.setRotationCenter(0, 0);
					m_printText.setRotation(270.0f);
					break;
				case LEFT:
					m_position = new Vector2(m_positionXs[3], m_positionYs[3]);
					m_printText.setRotationCenter(0, 0);
					m_printText.setRotation(90.0f);
					break;
				}
			}
			else
			{
				((RollerPhysicsWorld)((Roller)m_game).getPhysicWorld()).unregisterUpdateHandler(this);
				m_scene.detachChild(m_printText);
			}
		}
		
		Camera tempCamera = Roller.cameraManager.getCamera();
    	setByCameraPosition(tempCamera.getCenterX(), tempCamera.getCenterY());
    	
	}

	@Override
	public void reset() {
		// TODO Auto-generated method stub
		
	}
	public void trigger()
	{
		
		switch (Roller.nowDirection)
		{
		case UP:
			m_position = new Vector2(m_positionXs[0], m_positionYs[0]);
			m_printText.setRotationCenter(0, 0);
			m_printText.setRotation(0.0f);
			break;
		case DOWN:
			m_position = new Vector2(m_positionXs[2], m_positionYs[2]);
			m_printText.setRotationCenter(0, 0);
			m_printText.setRotation(180.0f);
			break;
		case RIGHT:
			m_position = new Vector2(m_positionXs[1], m_positionYs[1]);
			m_printText.setRotationCenter(0, 0);
			m_printText.setRotation(270.0f);
			break;
		case LEFT:
			m_position = new Vector2(m_positionXs[3], m_positionYs[3]);
			m_printText.setRotationCenter(0, 0);
			m_printText.setRotation(90.0f);
			break;
		}
		m_scene.detachChild(m_printText);
		m_scene.attachChild(m_printText);
		m_triggered = true;
		LoopEntityModifier EntityModifier = new LoopEntityModifier(0, new SequenceEntityModifier(new AlphaModifier(1, 0.0001f, 1.0f),
				                                                                                 new AlphaModifier(3, 1.0f, 1.0f),
				                                                                                 new AlphaModifier(1, 1, 0.0f)));
		m_printText.setVisible(true);
		m_printText.registerEntityModifier(EntityModifier);
	}
	public void setByCameraPosition(float cameraX, float cameraY) {
		m_printText.setPosition(cameraX + m_position.x - Roller.CAMERA_WIDTH / 2.0f,
				 			    cameraY + m_position.y - Roller.CAMERA_HEIGHT / 2.0f);
		
	}

}
